package com.mcode.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mcode.blog.dao.mapper.CommentMapper;
import com.mcode.blog.dao.pojo.Comment;
import com.mcode.blog.dao.pojo.SysUser;
import com.mcode.blog.service.CommentsService;
import com.mcode.blog.service.SysUserService;
import com.mcode.blog.utils.UserThreadLocal;
import com.mcode.blog.vo.ArticleVo;
import com.mcode.blog.vo.CommentVo;
import com.mcode.blog.vo.params.CommentParams;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private SysUserService sysUserService;


    @Override
    public List<CommentVo> getCommentByArticleId(Long articleId) {
        /**
         * 1. 根据文章id 查询 1级 评论列表 从 comment 表中查询
         * 2. 根据作者的id 查询作者的信息
         * 3. 判断 如果 level = 1 要去查询它有没有子评论
         * 4. 如果有 根据评论id 进行查询 （parent_id）
         */
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getLevel, 1);
        queryWrapper.eq(Comment::getArticleId, articleId);
        List<Comment> comments = commentMapper.selectList(queryWrapper);

        return copyList(comments);
    }


    /**
     * 评论
     *
     * @param commentParams
     */
    @Override
    public void comment(CommentParams commentParams) {
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParams.getArticleId());
        comment.setContent(commentParams.getContent());
        comment.setCreateDate(System.currentTimeMillis());

        Long parent = commentParams.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        } else {
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        comment.setAuthorId(sysUser.getId());
        Long toUserId = commentParams.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        commentMapper.insert(comment);
    }


    public List<CommentVo> copyList(List<Comment> commentList) {
        // 定义一个list
        List<CommentVo> commentoList = new ArrayList<>();
        for (Comment record : commentList) {
            commentoList.add(copy(record));
        }
        return commentoList;
    }


    public CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        // 把commet 赋值 给 commentVO 返回
        BeanUtils.copyProperties(comment, commentVo);

        // 查询作者信息
        Long authorId = comment.getAuthorId();
        commentVo.setAuthor(sysUserService.findUserVoById(authorId));

        // 子评论
        Integer level = comment.getLevel();
        if (1 == level) {
            Long id = comment.getId();
            commentVo.setChildrens(findCommentsByPid(id));
        }

        // 给谁评论
        if (level > 1) {
            Long toUid = comment.getToUid();
            commentVo.setToUser(sysUserService.findUserVoById(toUid));
        }

        return commentVo;
    }

    /**
     * 根据上级id查询下级评论
     *
     * @param id
     * @return
     */
    private List<CommentVo> findCommentsByPid(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId, id);
        // 只查询到2级
        queryWrapper.eq(Comment::getLevel, 2);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        return copyList(comments);
    }

}
